package com.example.photography.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.bean.Product_bean;
import com.example.photography.ui.CircleImageView;
import com.example.photography.util.UIUtils;
import com.example.photography.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 评论listview
 */
public abstract class Item_commentAdapter extends BaseExpandableListAdapter {
    boolean isLike = false;
    private Context context;
    private List<Product_bean.CommentsBean> commentsBeans;

    public Item_commentAdapter(List<Product_bean.CommentsBean> commentsBeans, Context context) {
        this.commentsBeans = commentsBeans;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return commentsBeans == null ? 0 : commentsBeans.size();
    }

    @Override
    public int getChildrenCount(int i) {
//        if (commentsBeans.get(i).getReplys() == null) {
//            return 0;
//        } else {
//            return Math.max(commentsBeans.get(i).getReplys().size(), 0);
//        }

        //默认展开两条信息
        if (commentsBeans.get(i).getReplys() == null || commentsBeans.get(i).getReplys().size() <= 0) {
            return 0;
        } else {
            return commentsBeans.get(i).getReplys().size() > 2 ? 3 : commentsBeans.get(i).getReplys().size();
//            return Math.min(commentsBeans.get(i).getReplys().size(), 2);
        }
    }

    @Override
    public Object getGroup(int i) {
        return commentsBeans.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return commentsBeans.get(i).getReplys().get(i1);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return getCombinedChildId(groupPosition, childPosition);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }


    @Override
    public View getGroupView(final int groupPosition, boolean isExpand, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_comment, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //根据位置获取信息
        Product_bean.CommentsBean comment = commentsBeans.get(groupPosition);
        //组件加载数据
        Glide.with(context)
                .load(comment.getObserver_head())
                .thumbnail(Glide.with(UIUtils.getContext()).load(R.drawable.head_empty))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(viewHolder.commentItemHead);
        viewHolder.commentItemUserName.setText(comment.getObserver_name());
        viewHolder.commentItemContent.setText(comment.getContent());
        viewHolder.commentItemTime.setText(Util.getTimeFormatText(comment.getTime()));
        viewHolder.tvLikes.setText(comment.getLikes() + "");
        ViewHolder finalViewHolder = viewHolder;
        /**
         * 点赞点击监听
         */
        viewHolder.ivLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLike) {
                    isLike = false;
                    finalViewHolder.ivLike.setImageResource(R.drawable.icon_work_like);
                    finalViewHolder.tvLikes.setText((comment.getLikes() - 1) + "");
                } else {
                    isLike = true;
                    finalViewHolder.ivLike.setImageResource(R.drawable.icon_work_like_true);
                    finalViewHolder.tvLikes.setText((comment.getLikes() + 1) + "");
                }
            }
        });
        /**
         * 头像点击监听
         */
        viewHolder.commentItemHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                headListen();
            }
        });

        return convertView;
    }
    final int TYPE_comment = 0;//回复内容
    final int TYPE_more = 1;//查看更多

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        if (childPosition < 2){
            return TYPE_comment;
        }else {
            return TYPE_more;
        }
    }

    @Override
    public int getChildTypeCount() {
        return 2;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        ChildHolder childHolder = null;
        ChildHolder2 childHolder2 = null;
        int type = getChildType(groupPosition,childPosition);
        if (convertView == null){
            switch (type) {
                case TYPE_comment:
                    convertView = LayoutInflater.from(context).inflate(R.layout.comment_reply_item_layout, viewGroup, false);
                    childHolder = new ChildHolder(convertView);
                    convertView.setTag(childHolder);
                    break;
                case TYPE_more:
                    convertView = LayoutInflater.from(context).inflate(R.layout.tv_reply, viewGroup, false);
                    childHolder2 = new ChildHolder2(convertView);
                    convertView.setTag(childHolder2);
                    break;
            }
        }else {
            switch (type) {
                case TYPE_comment:
                    childHolder = (ChildHolder) convertView.getTag();
                    break;
                case TYPE_more:
                    childHolder2 = (ChildHolder2) convertView.getTag();
                    break;
            }
        }


        switch (type) {
            case TYPE_comment:
                Product_bean.CommentsBean.ReplysBean replysBean = commentsBeans.get(groupPosition).getReplys().get(childPosition);
                //组件加载数据
                Glide.with(context)
                        .load(replysBean.getReply_head())
                        .thumbnail(Glide.with(UIUtils.getContext()).load(R.drawable.head_empty))
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(childHolder.commentItemHead);
                childHolder.tvReplyName.setText(replysBean.getReply_name());
                childHolder.tvTargetName.setText(replysBean.getTarget_name());
                childHolder.replyItemContent.setText(replysBean.getContent());
                childHolder.replyItemTime.setText(Util.getTimeFormatText(replysBean.getTime()));
                childHolder.tvLikes.setText(replysBean.getLikes() + "");
                ChildHolder finalViewHolder = childHolder;
                if (replysBean.getPc_id().equals(replysBean.getTarget_id())){
                    childHolder.tvTargetName.setVisibility(View.INVISIBLE);
                    childHolder.ivArrow.setVisibility(View.INVISIBLE);
                }else {
                    childHolder.tvTargetName.setVisibility(View.VISIBLE);
                    childHolder.ivArrow.setVisibility(View.VISIBLE);
                }
                /**
                 * 点赞点击监听
                 */
                finalViewHolder.ivLike.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (isLike) {
                            isLike = false;
                            finalViewHolder.ivLike.setImageResource(R.drawable.icon_work_like);
                            finalViewHolder.tvLikes.setText((replysBean.getLikes()) + "");
//                            finalViewHolder.tvLikes.setText((replysBean.getLikes() - 1) + "");
                        } else {
                            isLike = true;
                            finalViewHolder.ivLike.setImageResource(R.drawable.icon_work_like_true);
                            finalViewHolder.tvLikes.setText((replysBean.getLikes() + 1) + "");

                        }
                    }
                });
                break;
            case TYPE_more:
                childHolder2.tvMore.setText("查看全部"+commentsBeans.get(groupPosition).getReplys().size()+"条回复 >");
                childHolder2.tvMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        reply_more(commentsBeans.get(groupPosition).getPc_id());
                    }
                });
                break;
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    /**
     * by moos on 2018/04/20
     * func:评论成功后插入一条数据
     *
     * @param comment 新的评论数据
     */
    public void addTheCommentData(Product_bean.CommentsBean comment) {
        if (comment != null) {

            commentsBeans.add(comment);
            notifyDataSetChanged();
        } else {
            throw new IllegalArgumentException("评论数据为空!");
        }
    }

    /**
     * by moos on 2018/04/20
     * func:回复成功后插入一条数据
     *
     * @param replyListBean 新的回复数据
     */
    public void addTheReplyData(Product_bean.CommentsBean.ReplysBean replyListBean, int groupPosition) {
        if (replyListBean != null) {
            if (commentsBeans.get(groupPosition).getReplys() != null) {
                commentsBeans.get(groupPosition).getReplys().add(replyListBean);
            } else {
                List<Product_bean.CommentsBean.ReplysBean> replyList = new ArrayList<>();
                replyList.add(replyListBean);
                commentsBeans.get(groupPosition).setReplys(replyList);
            }
            notifyDataSetChanged();
        } else {
            throw new IllegalArgumentException("回复数据为空!");
        }

    }

    /**
     * by moos on 2018/04/20
     * func:添加和展示所有回复
     *
     * @param replyBeanList 所有回复数据
     * @param groupPosition 当前的评论
     */
    private void addReplyList(List<Product_bean.CommentsBean.ReplysBean> replyBeanList, int groupPosition) {
        if (commentsBeans.get(groupPosition).getReplys() != null) {
            commentsBeans.get(groupPosition).getReplys().clear();
            commentsBeans.get(groupPosition).getReplys().addAll(replyBeanList);
        } else {

            commentsBeans.get(groupPosition).setReplys(replyBeanList);
        }

        notifyDataSetChanged();
    }



    static
    class ViewHolder {
        @BindView(R.id.comment_item_head)
        CircleImageView commentItemHead;
        @BindView(R.id.comment_item_userName)
        TextView commentItemUserName;
        @BindView(R.id.iv_comment)
        ImageView ivComment;
        @BindView(R.id.iv_like)
        ImageView ivLike;
        @BindView(R.id.tv_likes)
        TextView tvLikes;
        @BindView(R.id.comment_item_content)
        TextView commentItemContent;
        @BindView(R.id.comment_item_time)
        TextView commentItemTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class ChildHolder {
        @BindView(R.id.comment_item_head)
        CircleImageView commentItemHead;
        @BindView(R.id.tv_reply_name)
        TextView tvReplyName;
        @BindView(R.id.tv_target_name)
        TextView tvTargetName;
        @BindView(R.id.iv_arrow)
        ImageView ivArrow;
        @BindView(R.id.reply_item_content)
        TextView replyItemContent;
        @BindView(R.id.reply_item_time)
        TextView replyItemTime;
        @BindView(R.id.iv_comment)
        ImageView ivComment;
        @BindView(R.id.iv_like)
        ImageView ivLike;
        @BindView(R.id.tv_likes)
        TextView tvLikes;

        ChildHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    class ChildHolder2 {
        @BindView(R.id.tv_more)
        TextView tvMore;

        ChildHolder2(View view) {
            ButterKnife.bind(this, view);
        }
    }


    //
//    @Override
//    public View myGetView(int position, View convertView) {
//        ViewHolder viewHolder = null;
//        if (convertView == null) {
//            convertView = View.inflate(mContext, R.layout.item_comment, null);
//            viewHolder = new ViewHolder(convertView);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//        //根据位置获取信息
//        Product_contentBean.CommentsBean comment = (Product_contentBean.CommentsBean) list.get(position);
//        //组件加载数据
//        Glide.with(mContext)
//                .load(comment.getHead_url())
//                .thumbnail(Glide.with(UIUtils.getContext()).load(R.drawable.head_empty))
//                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
//                .into(viewHolder.commentItemHead);
//        viewHolder.commentItemUserName.setText(comment.getName());
//        viewHolder.commentItemContent.setText(comment.getContent());
//        Date date = null;
//        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            Log.e("comment.getTime()",comment.getTime());
//            date = simpleDateFormat.parse(comment.getTime());
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        viewHolder.commentItemTime.setText(Util.getTimeFormatText(date));
//        ViewHolder finalViewHolder = viewHolder;
//        /**
//         * 点赞点击监听
//         */
//        viewHolder.commentItemLike.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if(isLike){
//                    isLike = false;
//                    finalViewHolder.commentItemLike.setImageResource(R.drawable.icon_comment_like);
//                }else {
//                    isLike = true;
//                    finalViewHolder.commentItemLike.setImageResource(R.drawable.icon_comment_love);
//                }
//            }
//        });
//        /**
//         * 头像点击监听
//         */
//        viewHolder.commentItemHead.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                headListen();
//            }
//        });
//
//        return convertView;
//    }
//
//    /**
//     * by moos on 2018/04/20
//     * func:评论成功后插入一条数据
//     *
//     * @param comment 新的评论数据
//     */
//    public void addTheCommentData(Product_contentBean.CommentsBean comment) {
//        if (comment != null) {
//
//            list.add(comment);
//            notifyDataSetChanged();
//        } else {
//            throw new IllegalArgumentException("评论数据为空!");
//        }
//    }
//
//
//    static
//    class ViewHolder {
//        @BindView(R.id.comment_item_head)
//        CircleImageView commentItemHead;
//        @BindView(R.id.comment_item_userName)
//        TextView commentItemUserName;
//        @BindView(R.id.comment_item_time)
//        TextView commentItemTime;
//        @BindView(R.id.comment_item_like)
//        ImageView commentItemLike;
//        @BindView(R.id.comment_item_content)
//        TextView commentItemContent;
//
//        ViewHolder(View view) {
//            ButterKnife.bind(this, view);
//        }
//    }
    public abstract void headListen();
    public abstract void reply_more(String pc_id);



}

